#include "Render/CgShaderManager.h"
#include "Render/CgShader.h"

#include <cstdlib>

namespace vital {

	//--------------------------------------------------------------------------
	TCgShaderManager::TCgShaderManager() {

		mCgContext = cgCreateContext();
		cgGLRegisterStates(mCgContext);
		cgGLSetManageTextureParameters(mCgContext, CG_TRUE);

	}

	//--------------------------------------------------------------------------
	TCgShaderManager::~TCgShaderManager() {

		cgDestroyContext(mCgContext);
	}

	//--------------------------------------------------------------------------
	IShader* TCgShaderManager::createShader(const TStr& filename) {

		CGeffect effect = cgCreateEffectFromFile(mCgContext, filename.c_str(), NULL);

		CGtechnique tech = cgGetFirstTechnique(effect);

		while (tech && cgValidateTechnique(tech) == CG_FALSE) {
			tech = cgGetNextTechnique(tech);
		}

		if(tech) {
			return new TCgShader(tech);
		}

		return NULL;
	}

	//--------------------------------------------------------------------------
}